#ifndef QPICKRESULTMODEL_H
#define QPICKRESULTMODEL_H

#include <QAbstractItemModel>
#include "../util/KtGroupedVector.h"

class QcPickResultTreeModel;

// tree代理模型，不实际存储数据，使用QcPickResultListModel的数据

class QcPickResultListModel : public QAbstractItemModel
{
    Q_OBJECT

public:
    explicit QcPickResultListModel(QcPickResultTreeModel* treeModel, QObject *parent = nullptr);

    // Header:
    QVariant headerData(int section,
                        Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const override;

    // Basic functionality:
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
    QModelIndex parent(const QModelIndex &index) const override;

    int rowCount(const QModelIndex &parent = QModelIndex()) const override;
    int columnCount(const QModelIndex &parent = QModelIndex()) const override;

    Qt::ItemFlags flags(const QModelIndex &index) const override;

    bool hasChildren(const QModelIndex &parent) const override;

    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;

    void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;

    bool isGroupped() const; // 扫描结果是否分组？

    bool isGroupRoot(int row) const; // 第@row行数据是否为分组根节点

    int groupCount() const { return gv_.groups(); }

    int groupSize(int groupIdx) const;

    int itemAmount() const;

    // 返回第@groupIdx分组、第@itemIdx个项目的行序号
    // 若@itemIdx=-1，返回@groupIdx分组根节点所在行序号
    // 若@groupIdx=-1，表示无分组模式，返回@itemIdx
    int listRow(int groupIdx, int itemIdx) const;

    // 返回listRow所在的group序号和分组下的item序号
    //   group序号<0，表示非分组模式，此时返回的item序号等于listRow
    //   item序号<0，表示listRow为group分组的根节点
    QPair<int, int> groupIndex(int listRow) const;

    void appendItem(int group, const QString& filePath);

    void removeItem(int group, int item);

    void removeGroup(int group);

    bool empty() const;

private:

    QModelIndex toTreeIndex_(const QModelIndex &listIndex) const;

    QModelIndex fromTreeIndex_(const QModelIndex &treeIndex) const;

    // 设置@group分组的所有子节点check状态为@checked
    void setChildrenCheckStates_(int group, bool checked);

private:
    QcPickResultTreeModel* treeModel_;
    KtGroupedVector<> gv_; // 用来同步item位置信息，不存储item数据
};

#endif // QPICKRESULTMODEL_H
